//
//  PriceStarView.swift
//  AirTravel
//
//  Created by apple on 2021/7/15.
//

import UIKit

protocol PriceStarDelegate:NSObjectProtocol {
    func priceStarClick(priceDic:Dictionary<String,Any>,starDic:Dictionary<String,Any>)
}

class PriceStarView: UIView {
    var contentView:UIView!
    weak var delegate: PriceStarDelegate?
    @IBOutlet weak var collView: UICollectionView!
    
    /// 完成
    @IBOutlet weak var completeBtn: UIButton!
    /// 清空
    @IBOutlet weak var emptyBtn: UIButton!
    let identifyPR = "PriceReusableView"
    let identifyPS = "PriceStarColl"
    var dataSoce : Array<Any>?
    var priceDic : Dictionary<String,Any>?
    var starDic : Dictionary<String,Any>?
    
    
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
        
        self.emptyBtn.layer.cornerRadius = 6
        self.emptyBtn.layer.borderWidth = 1
        self.emptyBtn.layer.borderColor = UIColor.init(r: 212, g: 212, b: 212).cgColor
        self.emptyBtn.addTarget(self, action: #selector(empty), for: .touchUpInside)
        
        self.completeBtn.layer.cornerRadius = 6
        self.completeBtn.layer.masksToBounds = true
        self.completeBtn.addTarget(self, action: #selector(complete), for: .touchUpInside)
        
        self.collView.dataSource = self
        self.collView.delegate = self
        let layout1 = UICollectionViewFlowLayout.init()
        layout1.scrollDirection = .vertical
        
        layout1.itemSize = .init(width: (SCREEN_WIDTH - 26 - 18) / 4, height: 40)
        layout1.minimumInteritemSpacing = 6
        layout1.sectionInset = UIEdgeInsets.init(top: 6, left: 13, bottom: 6, right: 13)
        self.collView.collectionViewLayout = layout1
        self.collView.register(UINib.init(nibName: "PriceReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: identifyPR)
        self.collView.register(UINib.init(nibName: "PriceStarColl", bundle: nil), forCellWithReuseIdentifier: identifyPS)
    }
    
    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    //清空
    @objc func empty() {
        self.priceDic = nil
        self.starDic = nil
        self.collView.reloadData()
    }
    //提交
    @objc func complete() {
        self.delegate?.priceStarClick(priceDic: self.priceDic ?? ["keyword":"","min":"","max":""], starDic: self.starDic ?? ["id":"","name":""])
        GKCover.hideWithoutAnimation()
    }
    
    func setUpData(array:Array<Any>) {
        self.dataSoce = array
        self.collView.reloadData()
    }
}

extension PriceStarView:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int{
        return self.dataSoce?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let array = self.dataSoce {
            let dic = array[section] as? Dictionary<String, Any>
            let jgs = dic?["infolist"] as? Array<Any>
            return jgs?.count ?? 0
        }
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifyPS, for: indexPath) as! PriceStarColl
        if let array = self.dataSoce {
            let dic = array[indexPath.section] as? Dictionary<String, Any>
            let keyword = "\(dic?["keyword"] ?? "")"
            cell.backView.backgroundColor = UIColor.groupTableViewBackground
            if keyword == "价格" {
                let jgs = dic?["infolist"] as? Array<Any>
                if let jgarray = jgs {
                    let dict = jgarray[indexPath.row] as? Dictionary<String,Any>
                    let keyword = "\(dict?["keyword"] ?? "")"
                    if let priceDic = self.priceDic {
                        let keyword1 = "\(priceDic["keyword"] ?? "")"
                        if keyword == keyword1 {
                            cell.backView.backgroundColor = Yellow_COLOR
                        }else{
                            cell.backView.backgroundColor = UIColor.groupTableViewBackground
                        }
                    }
                    cell.contentLab.text = keyword
                }
                
            }else if keyword == "星级"{
                let jgs = dic?["infolist"] as? Array<Any>
                if let jgarray = jgs {
                    let dict = jgarray[indexPath.row] as? Dictionary<String,Any>
                    let name = "\(dict?["name"] ?? "")"
                    if let starDic = self.starDic {
                        let name1 = "\(starDic["name"] ?? "")"
                        if name == name1 {
                            cell.backView.backgroundColor = Yellow_COLOR
                        }else{
                            cell.backView.backgroundColor = UIColor.groupTableViewBackground
                        }
                    }
                    cell.contentLab.text = "\(dict?["name"] ?? "")"
                }
            }
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        //1.取出section的headerView
        let headerView : PriceReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifyPR, for: indexPath) as! PriceReusableView
        if let array = self.dataSoce {
            let dic = array[indexPath.section] as? Dictionary<String, Any>
            let keyword = "\(dic?["keyword"] ?? "")"
            headerView.titleLab.text = "\(keyword)"
        }
        return headerView
    }
    
    //header高度
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: SCREEN_WIDTH, height: 40)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        if let array = self.dataSoce {
            let dic = array[indexPath.section] as? Dictionary<String, Any>
            let keyword = "\(dic?["keyword"] ?? "")"
            if keyword == "价格" {
                let jgs = dic?["infolist"] as? Array<Any>
                if let jgarray = jgs {
                    let dict = jgarray[indexPath.row] as? Dictionary<String,Any>
                    self.priceDic = dict
                }
                
            }else if keyword == "星级"{
                let jgs = dic?["infolist"] as? Array<Any>
                if let jgarray = jgs {
                    let dict = jgarray[indexPath.row] as? Dictionary<String,Any>
                    self.starDic = dict
                }
            }
            self.collView.reloadData()
        }
    }
}
